package com.M10.BE.fragment;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.M10.BE.R;
import com.M10.BE.adapter.NInveLoseAdapter;
import com.M10.BE.adapter.NNewAdapter;
import com.M10.BE.database.TableAsset;
import com.M10.BE.database.TableInventory;
import com.M10.BE.entity.NAsset;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;

/**
 * 资产盘点页面的adapter
 *
 * @author cuter
 * @date 2012-8-20下午02:33:11
 * @Copyright CHANCE
 */
public class ToCheckFragment extends BaseFragment implements OnClickListener, FragmentLifecycle {
    private Context context;

    private ArrayList<NAsset> assetLists = new ArrayList<NAsset>();
    private ListView listView;
    NInveLoseAdapter nNewAdapter = null;
    View view = null;
    View header;
    public static Handler mHandler = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity(); mHandler = sHandler;
    }

    protected void initView(View view, LayoutInflater inflater) {
        listView = (ListView) view.findViewById(R.id.listview_fragment);
        header = inflater.inflate(R.layout.header_item_checked, null);
        nNewAdapter = new NInveLoseAdapter(context, assetLists,
                listView);
        listView.addHeaderView(header);
        listView.setAdapter(nNewAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.listview_asset_fragment;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        new  ReadUncheck(context).execute();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
    }
    private Handler sHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            List<NAsset> list = (List<NAsset>) msg.obj;
            nNewAdapter.setList(list);
            nNewAdapter.notifyDataSetChanged();
        }
    };
    private class ReadUncheck extends AsyncTask<Void, Void, List<NAsset>> {
        private Context context;

        public ReadUncheck(Context context) {
            this.context = context;
        }

        @Override
        protected List<NAsset> doInBackground(Void... voids) {
            List<NAsset> assets = TableAsset.searchAsset(context);
            return assets;
        }

        @Override
        protected void onPostExecute(List<NAsset> nAssets) {
            if (nNewAdapter != null) {
                nNewAdapter.setList(nAssets);
                nNewAdapter.notifyDataSetChanged();
            }
            super.onPostExecute(nAssets);
        }
    }

    /**
     * 这个类用来异步加载陌生人和mark的的信息
     *
     * @author Muhuan
     */

    @Override
    public void onPauseFragment() {
    }

    @Override
    public void onResumeFragment() {
    }

    @Override
    public void onPostFragment() {
    }
}
